from utils.convert import (
    rj_imglist,
    header_placeholder,
    add_ad_service,
    footer_placeholder,
)
from website.tzy.index import raw_proinfo
from bs4 import BeautifulSoup


def create_ad_dom():
    """创建天正源广告DOM结构

    该广告需要在4月份下架
    """
    soup = BeautifulSoup("", "html.parser")
    # 介绍页
    img_brochure_1 = soup.new_tag(
        "img",
        attrs={
            "src": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/tzy/tzy_brochure_1.jpg"
        },
    )
    img_brochure_2 = soup.new_tag(
        "img",
        attrs={
            "src": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/tzy/tzy_brochure_2.jpg"
        },
    )
    # 广告
    div_ad = soup.new_tag("div", attrs={"class": "tzy_ad_img"})
    img = soup.new_tag(
        "img",
        attrs={
            "src": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/tzy/tzy_ad.jpg"
        },
    )
    div_ad.append(img)
    soup.append(div_ad)  # 直接将新的 div 追加到 soup 中
    soup.append(img_brochure_1)  # 直接将新的 div 追加到 soup 中
    soup.append(img_brochure_2)  # 直接将新的 div 追加到 soup 中
    return soup.prettify()


def process_proinfo(procode: str) -> dict:
    """将数据转换为 rj 基础信息

    Args:
        procode (str): 货号

    Returns:
        _type_: _description_
    """
    _temp_data = raw_proinfo(procode)
    if _temp_data:
        _temp_data["desc"] = (
            header_placeholder()
            + add_ad_service()
            + _temp_data["desc"]
            + create_ad_dom()
            + footer_placeholder()
        )
        _temp_data["photos"] = rj_imglist(_temp_data["photos"])
        _temp_data["brandId"] = 6963
        _temp_data["categoryId"] = 231
        _temp_data["unit"] = "支"
        _temp_data["deliveryTime"] = 14
        _temp_data["status"] = 3
    return _temp_data


def process_spec(prospec: str) -> list:
    """创建价格和规格列表

    Args:
        prospec (str): 组合字符串

    Returns:
        list: _description_
    """
    # 处理字符串并转换为列表
    result = []
    # 首先按|分割成不同规格
    items = prospec.split("|")

    for item in items:
        # 去除转义字符\，并按/分割规格和价格
        spec, price = item.replace("\\", "").split("/")
        # 创建字典并添加到结果列表中
        result.append({"spec": spec, "price": int(price)})

    return result


def sku_generator(item: dict) -> list:
    """生成sku"""
    sku_list = []
    for _item in process_spec(item["spec_list"]):
        sku_list.append(
            {
                "code": item["code"] + "-" + _item["spec"],
                "price": _item["price"],
                "directoryPrice": 1.9 * _item["price"],
                "stock": 999,
                "carryFeeTemplateId": "4611",
                "skuAttributes": [
                    {
                        "attributeName": "产品规格",
                        "attributeValue": _item["spec"],
                    }
                ],
                "deliveryTime": 14,
                "unit": "支",
            }
        )
    return sku_list


def gen_upload_dict(item: dict, product: dict) -> dict:
    """生成 rj 多规格商品上传数据

    Args:
        item (dict): 商品基础item 数据
        product (dict): 商品规格价格数据

    Returns:
        dict: 待上传多规格商品数据
    """
    sku_list = []
    for _item in process_spec(product["spec_list"]):
        sku_list.append(
            {
                "code": item["code"] + "-" + _item["spec"],
                "price": _item["price"],
                "directoryPrice": 1.9 * _item["price"],
                "stock": 999,
                "carryFeeTemplateId": "4611",
                "skuAttributes": [
                    {
                        "attributeName": "产品规格",
                        "attributeValue": _item["spec"],
                    }
                ],
                "deliveryTime": 14,
                "unit": "支",
            }
        )
    item["skuInfos"] = sku_list
    return item
